package cn.waka.sys.admin.service;

import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@Component
public class RedisService {

	@Autowired
	private JedisPool pool;
	
	public void setPool(JedisPool pool){
		this.pool=pool;
	}
	
	public void incr(String key){
		Jedis r = pool.getResource();
		try{
			r.incr(key);
		}finally{
			r.close();
		}
	}
	public void decr(String key){
		Jedis r = pool.getResource();
		try{
			r.decr(key);
		}finally{
			r.close();
		}
	}
	
	public void incrby(String key,int num){
		Jedis r = pool.getResource();
		try{
			r.incrBy(key,num);
		}finally{
			r.close();
		}
	}
	public void decrby(String key,int num){
		Jedis r = pool.getResource();
		try{
			r.decrBy(key,num);
		}finally{
			r.close();
		}
	}
	
	
	public String get(String key){
		Jedis r = pool.getResource();
		try{
			return r.get(key);
		}finally{
			r.close();
		}
		
	}
	
	public void expire(String key,int time){
		Jedis r = pool.getResource();
		try{
			r.expire(key, time);
		}finally{
			r.close();
		}
		
	}
	
	public void set(String key,String value){
		Jedis r = pool.getResource();
		try{
			r.set(key, value);
		}finally{
			r.close();
		}
	}
	public boolean exists(String key){
		Jedis r = pool.getResource();
		try{
			return r.exists(key);
		}finally{
			r.close();
		}
	}
	public boolean setnx(String key,String value){
		Jedis r = pool.getResource();
		try{
			return r.setnx(key, value)>0;
		}finally{
			r.close();
		}
	}
	
	public void publish(String key,String value){
		Jedis r = pool.getResource();
		try{
			r.publish(key, value);
		}finally{
			r.close();
		}
	}
	
	public void del(String key){
		Jedis r = pool.getResource();
		try{
			r.del(key);
		}finally{
			r.close();
		}
	}
	public void dels(final String[] keys){
		Jedis r = pool.getResource();
		try{
			r.del(keys);
		}finally{
			r.close();
		}
	}
	public Set<String> keys(String pre){
		Jedis r = pool.getResource();
		try{
			return r.keys(pre +"*");
		}catch(Exception ex){
			return null;
		}
		finally{
			r.close();
		}
	}
}
